package vn.cent.games.findnumber.beans;

import java.util.List;
import java.util.Timer;
import java.util.concurrent.ConcurrentHashMap;

import vn.cent.games.findnumber.main.FindNumberExtension;
import vn.cent.games.findnumber.utils.Constants;
import vn.cent.games.findnumber.utils.SyncGameStart;

import com.smartfoxserver.v2.entities.Room;
import com.smartfoxserver.v2.entities.User;

/**
 * GameBean: class describing a game match
 * 
 * @author TV.Vinh
 * @version 1.0
 *
 */

public class GameBean {
	/** correspond to room*/
	private Room room;
	
	/** user host the room */
	private int host;
	
	/** user join the room */
	private int guest;
	
//	/** Initial map associated to this game */
//	private GameMapBean gameMapBean;
	
	/** players, limit to 2 players */
	private ConcurrentHashMap<Integer, PlayerBean> players;
	
//	/** game start time */
//	private long gameStartTime;
	
	/** Game started flag */
	private boolean started = false;
	
	private Timer timer;
	
	// CONTRUCTOR
	
	public GameBean(Room room, int host) {
		this.room = room;
		this.host = host;
//		gameMapBean = new GameMapBean();
		players = new ConcurrentHashMap<Integer, PlayerBean>();
	}

	// GETTER & SETTER
	
	public Room getRoom() {
		return room;
	}

	public void setRoom(Room room) {
		this.room = room;
	}

	public int getHost() {
		return host;
	}

	public void setHost(int host) {
		this.host = host;
	}

	public int getGuest() {
		return guest;
	}

	public void setGuest(int guest) {
		this.guest = guest;
	}

//	public GameMapBean getGameMapBean() {
//		return gameMapBean;
//	}
//
//	public void setGameMapBean(GameMapBean gameMapBean) {
//		this.gameMapBean = gameMapBean;
//	}

	public ConcurrentHashMap<Integer, PlayerBean> getPlayers() {
		return players;
	}

	public void setPlayers(ConcurrentHashMap<Integer, PlayerBean> players) {
		this.players = players;
	}

//	public long getGameStartTime() {
//		return gameStartTime;
//	}
//
//	public void setGameStartTime(long gameStartTime) {
//		this.gameStartTime = gameStartTime;
//	}

	public boolean isStarted() {
		return started;
	}

	public void setStarted(boolean started) {
		this.started = started;
	}
	
	/** 
	 * Start a new game 
	 * time schedule delay for 5s then start game
	 */
	
	public void startGame(List<User> recipients, FindNumberExtension gameExt){
		// game will start after 5s
//		gameStartTime = System.currentTimeMillis() + Constants.DELAY_TIME;
		started = true;
		
		// Wait a number of seconds and then notify clients that the game starts!
		timer = new Timer();
		timer.schedule(new SyncGameStart(recipients, gameExt), Constants.DELAY_TIME);
	}
}
